﻿// Heading Indicator - 航向仪

#include "HeadingIndicator.h"

void drawHI(double x0, double y0, double radius, double current_heading)
{
    double width = x0 * 2;
    double height = y0 * 2;
    //double current_heading = 0;
    //initgraph(width, height, INIT_RENDERMANUAL);//手动渲染
    ege_enable_aa(true);//抗锯齿
    //    setbkcolor(WHITE);//背景

        //int radius = 195;
    double angle = 269 - current_heading;
    /*for (; is_run(); delay_fps(60))
    {*/
    //cleardevice();
    dialFrame(width, height, radius);//表盘框
    screw(radius, width / 2, height / 2);


    scale(width, height, radius, current_heading);//刻度和数字

    pointPlane(width, height, radius);//飞机

    pointOrange(width, height, radius);//橘黄色标记
    specialPointOrange(width, height, radius, angle);
    buttonhi(width, height, radius);
    //current_heading = update_heading(current_heading);//数据变化模拟


    //}


}


void dialFrame(double width, double height, double radius)
{
    double circle_x = width / 2;
    double circle_y = height / 2;
    double circle_r = radius;
    setfillcolor(EGERGB(102, 102, 102));//矩形颜色
    //ege_fillrect(width / 2 - radius * 1.16667, height / 2 - radius * 1.25, radius * 2.333, radius * 2.5);//矩形
    //填充外圆
    setfillcolor(EGERGB(55, 55, 55));
    ege_fillellipse(width / 2 - radius, height / 2 - radius, 2 * radius, 2 * radius);
    //内圆加痕
    //setcolor(EGEARGB(0xFF, 0x20, 0x20, 0x20));
    setcolor(EGERGB(30, 30, 30));
    setlinewidth(radius * 0.03333);//8
    ege_ellipse(width / 2 - radius, height / 2 - radius, 2 * radius, 2 * radius);

    //中间部分
    //setcolor(EGEARGB(0xFF, 0x33, 0x33, 0x33));
    setcolor(EGERGB(45, 45, 45));
    setlinewidth(radius * 0.12083);//29
    ege_ellipse(width / 2 - radius - radius * 0.04583, height / 2 - radius - radius * 0.04583, 2 * radius + radius * 0.09167, 2 * radius + radius * 0.09167);


    setcolor(EGERGB(20, 20, 20));
    setlinewidth(radius * 0.01666);//4
    ege_ellipse(width / 2 - radius + radius * 0.00833, height / 2 - radius + radius * 0.00833, 2 * radius, 2 * radius);//2  2  2*r   2*r


    setcolor(EGERGB(45, 45, 45));
    setlinewidth(radius * 0.02916);//7
    ege_ellipse(width / 2 - radius + radius * 0.02083, height / 2 - radius + radius * 0.02083, 2 * radius - radius * 0.04166, 2 * radius - radius * 0.04166);//5  5  -10  -10

    setcolor(EGERGB(15, 15, 15));
    setlinewidth(radius * 0.02916);//7
    ege_ellipse(width / 2 - radius + radius * 0.04166, height / 2 - radius + radius * 0.04166, 2 * radius - radius * 0.08333, 2 * radius - radius * 0.0833);//  10  10  -20  -20


    //外圆框
    setcolor(BLACK);
    setlinewidth(radius * 0.025);//6
    ege_ellipse(width / 2 - radius - radius * 0.1167, height / 2 - radius - radius * 0.1167, 2 * radius + radius * 0.1167 * 2, 2 * radius + radius * 0.1167 * 2);
    //记号
    setcolor(BLACK);//描边颜色
    setlinewidth(radius * 0.00625);//线条粗细//
    ege_circle(circle_x, circle_y, circle_r + circle_r / 10);//描边

    ege_point p5 = { circle_x - circle_r - circle_r / 10 + circle_r / 30 * 11 ,circle_y - circle_r - circle_r / 10 + circle_r / 30 * 11 };
    ege_setpattern_ellipsegradient(p5, EGERGB(160, 160, 160), circle_x - circle_r - circle_r / 10, circle_y - circle_r - circle_r / 10, 2 * (circle_r + circle_r / 10), 2 * (circle_r + circle_r / 10), EGEACOLOR(0xFF, 0x00161616));
    ege_fillrect(circle_x - circle_r - circle_r / 10, circle_y - circle_r - circle_r / 10, 2 * (circle_r + circle_r / 10), 2 * (circle_r + circle_r / 10));

    //最外圈


    setcolor(BLACK);//描边颜色
    setlinewidth(radius * 0.01666);//线条粗细//4
    ege_circle(circle_x, circle_y, circle_r + circle_r / 150);//描边

    ege_point p6 = { circle_x - circle_r - circle_r / 300 * 2 + circle_r / 30 * 9 ,circle_y - circle_r - circle_r / 300 * 2 + circle_r / 30 * 9 };
    ege_setpattern_ellipsegradient(p6, EGERGB(140, 140, 140), circle_x - circle_r - circle_r / 300 * 2, circle_y - circle_r - circle_r / 300 * 2, 2 * (circle_r + circle_r / 300 * 2), 2 * (circle_r + circle_r / 300 * 2), EGEACOLOR(0xFF, 0x003A3A3C));
    ege_fillrect(circle_x - circle_r - circle_r / 300 * 2, circle_y - circle_r - circle_r / 300 * 2, 2 * (circle_r + circle_r / 300 * 2), 2 * (circle_r + circle_r / 300 * 2));

    //setfillcolor(EGERGB(80, 80, 80));//填充为灰色
    //ege_fillcircle(circle_x, circle_y, circle_r + circle_r / 300 * 2);//圆形边框
//第二圈


    setlinewidth(radius * 0.03333);//线条粗细
    //setfillcolor(EGERGB(55, 55, 55));//填充为灰色


    ege_point p7 = { circle_x  ,circle_y };
    ege_setpattern_ellipsegradient(p7, EGERGB(65, 65, 65), circle_x - circle_r + circle_r / 30, circle_y - circle_r + circle_r / 30, 2 * (circle_r - circle_r / 30), 2 * (circle_r - circle_r / 30), EGEACOLOR(0xFF, 0x00292925));
    ege_fillrect(circle_x - circle_r + circle_r / 30, circle_y - circle_r + circle_r / 30, 2 * (circle_r - circle_r / 30), 2 * (circle_r - circle_r / 30));
    ege_circle(circle_x, circle_y, circle_r - circle_r / 30);//描边

    //ege_fillcircle(circle_x, circle_y, circle_r - circle_r / 30);
    //setfillcolor(EGERGB(38, 38, 38));//填充为深灰色
//第三圈

}

//表盘中的飞机模型
void pointPlane(double width, double height, double radius)
{
    ege_point points[] = { width / 2,height / 2 - radius + radius * 0.24167 ,   //58
                           width / 2 + radius * 0.05,height / 2 - radius * 0.3,//12  72
                           width / 2,height / 2 - radius * 0.235,//  0   56
                           width / 2 - radius * 0.05,height / 2 - radius * 0.3 };//-12 72
    setfillcolor(EGERGB(190, 190, 190));
    ege_fillpoly(4, points);
    //末尾添加的是偏差调整如果圆的半径过大多少倍相应也扩大多少倍
    //飞机身体
    ege_point points1[] = { width / 2 - radius * 0.00833,height / 2 - radius * 0.21667 ,//-2  -52
                            width / 2 + radius * 0.00833,height / 2 - radius * 0.21667,//2  -52
                            width / 2 + radius * 0.00625,height / 2 - radius * 0.20417,//1.5  -49
        //width / 2,height / 2 - 42,
                        width / 2 + radius * 0.0357,height / 2 - radius * 0.17083,// 9  -41
                        width / 2 + radius * 0.0357,height / 2 - radius * 0.09583,//9  -23
                        width / 2 + radius * 0.3125,height / 2 - radius * 0.09583,//75  -23
                        width / 2 + radius * 0.3125,height / 2 - radius * 0.02083,//75  -5
                        width / 2 + radius * 0.14167,height / 2 + radius * 0.02083,//34  5
                        width / 2 + radius * 0.02916,height / 2 + radius * 0.02083,//7  5
                        width / 2 + radius * 0.01666,height / 2 + radius * 0.16666,//4  40
                        width / 2 + radius * 0.10416,height / 2 + radius * 0.1857,//25  45
                        width / 2 + radius * 0.10416,height / 2 + radius * 0.22916,//25 55
                        width / 2 + radius * 0.01666,height / 2 + radius * 0.25,//4  60
                        width / 2,height / 2 + radius * 0.22916,//0  55
                        width / 2 - radius * 0.01666,height / 2 + radius * 0.25,//-4  60
                        width / 2 - radius * 0.10416,height / 2 + radius * 0.22916,//-25  55
                        width / 2 - radius * 0.10416,height / 2 + radius * 0.1875,//-25  45
                        width / 2 - radius * 0.01666,height / 2 + radius * 0.16666,//-4  40
        //width/2,height/2+45,
                        width / 2 - radius * 0.02916,height / 2 + radius * 0.02083,//-7  5
                        width / 2 - radius * 0.14167,height / 2 + radius * 0.02083,//-34  5
                        width / 2 - radius * 0.3125,height / 2 - radius * 0.02083,//-75  -5
                        width / 2 - radius * 0.3125,height / 2 - radius * 0.09583,//-75  -23
                        width / 2 - radius * 0.0375,height / 2 - radius * 0.09583,//-9  -23
        //width / 2 + 9,height / 2 - 23,
        //width / 2 - 9,height / 2 - 23,
                        width / 2 - radius * 0.0375,height / 2 - radius * 0.17083 ,//-9  -41
                        width / 2 - radius * 0.00625,height / 2 - radius * 0.20417 };//-1.5  -49
    ege_fillpoly(25, points1);
}

//橘黄色箭头
void pointOrange(double width, double height, double radius)
{
    settextjustify(CENTER_TEXT, CENTER_TEXT);
    setcolor(WHITE);
    setfont(radius*0.08205128, 0, "Arial");//16
    setbkmode(TRANSPARENT);
    //底部直线
    outtextxy(width / 2, height / 2 + radius * 0.30833, "VACUUM");//  0  74
    ege_line(width / 2, height / 2 + radius * 0.35, width / 2, height / 2 + radius * 0.7666);//  0   84   0   184
    //橘黄色箭头
    setfillcolor(EGERGB(200, 75, 42));
    //底部
    ege_point points2[] = { width / 2,height / 2 + radius * 0.77083,width / 2 + radius * 0.04166,height / 2 + radius * 0.9375,width / 2 - radius * 0.04166,height / 2 + radius * 0.9375 };//  0  185  10  225  -10  225
    ege_fillpoly(3, points2);
    //前部
    ege_fillrect(width / 2 - radius * 0.0125, height / 2 - radius + radius * 0.05833, radius * 0.025, radius * 0.16666);//-3  14  6  40
    //正右
    ege_point points3[] = { width / 2 + radius * 0.775,height / 2,width / 2 + radius * 0.94166,height / 2 - radius * 0.04166,width / 2 + radius * 0.94166,height / 2 + radius * 0.04166 };//  186  0  226  -10  226  10
    ege_fillpoly(3, points3);
    //正左
    ege_point points4[] = { width / 2 - radius * 0.775,height / 2,width / 2 - radius * 0.94166,height / 2 - radius * 0.04166,width / 2 - radius * 0.94166,height / 2 + radius * 0.04166 };//  -186  0 -226  -10 -226 10
    ege_fillpoly(3, points4);
    //左上
    ege_point points5[] = { width / 2 - (radius * 0.9375 - radius * 0.0625) * cos(PI / 180 * 45),height / 2 - (radius * 0.9375 - radius * 0.0625) * sin(PI / 180 * 45),//(225-15)
                            width / 2 - radius * 0.9375 * cos(PI / 180 * 46.5) ,height / 2 - radius * 0.9375 * sin(PI / 180 * 46.5),//225
                            width / 2 - radius * 0.9375 * cos(PI / 180 * 43.5),height / 2 - radius * 0.9375 * sin(PI / 180 * 43.5) };
    ege_fillpoly(3, points5);
    //右上
    ege_point points6[] = { width / 2 + (radius * 0.9375 - radius * 0.0625) * cos(PI / 180 * 45),height / 2 - (radius * 0.9375 - radius * 0.0625) * sin(PI / 180 * 45),//(225-15)
                            width / 2 + radius * 0.9375 * cos(PI / 180 * 46.5) ,height / 2 - radius * 0.9375 * sin(PI / 180 * 46.5),//225
                            width / 2 + radius * 0.9375 * cos(PI / 180 * 43.5),height / 2 - radius * 0.9375 * sin(PI / 180 * 43.5) };
    ege_fillpoly(3, points6);
    //左下
    ege_point points7[] = { width / 2 - (radius * 0.9375 - radius * 0.0625) * cos(PI / 180 * 45),height / 2 + (radius * 0.9375 - radius * 0.0625) * sin(PI / 180 * 45),
                            width / 2 - radius * 0.9375 * cos(PI / 180 * 46.5) ,height / 2 + radius * 0.9375 * sin(PI / 180 * 46.5),
                            width / 2 - radius * 0.9375 * cos(PI / 180 * 43.5),height / 2 + radius * 0.9375 * sin(PI / 180 * 43.5) };
    ege_fillpoly(3, points7);
    //右下
    ege_point points8[] = { width / 2 + (radius * 0.9375 - radius * 0.0625) * cos(PI / 180 * 45),height / 2 + (radius * 0.9375 - radius * 0.0625) * sin(PI / 180 * 45),
                            width / 2 + radius * 0.9375 * cos(PI / 180 * 46.5) ,height / 2 + radius * 0.9375 * sin(PI / 180 * 46.5),
                            width / 2 + radius * 0.9375 * cos(PI / 180 * 43.5),height / 2 + radius * 0.9375 * sin(PI / 180 * 43.5) };
    ege_fillpoly(3, points8);
}
//特殊橘黄色箭头
void specialPointOrange(double width, double height, double radius, double angle) {

    setfillcolor(EGERGB(195, 75, 10));

    ege_point points9[] = { width / 2 + (radius * 0.93 - radius * 0.14) * cos(PI / 180 * angle),height / 2 + (radius * 0.93 - radius * 0.14) * sin(PI / 180 * angle) ,
                            width / 2 + (radius * 0.93 - radius * 0.14) * cos(PI / 180 * (angle - 2)),height / 2 + (radius * 0.93 - radius * 0.14) * sin(PI / 180 * (angle - 2)) ,
                            width / 2 + radius * 0.94 * cos(PI / 180 * (angle - 4)),height / 2 + radius * 0.94 * sin(PI / 180 * (angle - 4)),
                            width / 2 + radius * 0.94 * cos(PI / 180 * angle),height / 2 + radius * 0.94 * sin(PI / 180 * angle) };

    ege_point points10[] = { width / 2 + (radius * 0.93 - radius * 0.14) * cos(PI / 180 * (angle + 2)),height / 2 + (radius * 0.93 - radius * 0.14) * sin(PI / 180 * (angle + 2)) ,
                             width / 2 + radius * 0.94 * cos(PI / 180 * (angle + 2)),height / 2 + radius * 0.94 * sin(PI / 180 * (angle + 2)) ,
                             width / 2 + radius * 0.94 * cos(PI / 180 * (angle + 6)),height / 2 + radius * 0.94 * sin(PI / 180 * (angle + 6)),
                             width / 2 + (radius * 0.93 - radius * 0.14) * cos(PI / 180 * (angle + 4)),height / 2 + (radius * 0.93 - radius * 0.14) * sin(PI / 180 * (angle + 4)),
    };
    ege_fillpoly(4, points9);
    ege_fillpoly(4, points10);
    setlinewidth(radius * 0.0125);
    setcolor(EGERGB(150, 65, 25));
    ege_line(width / 2 + (radius * 0.93 - radius * 0.14) * cos(PI / 180 * angle), height / 2 + (radius * 0.93 - radius * 0.14) * sin(PI / 180 * angle), width / 2 + radius * 0.94 * cos(PI / 180 * angle), height / 2 + radius * 0.94 * sin(PI / 180 * angle));
    ege_line(width / 2 + (radius * 0.93 - radius * 0.14) * cos(PI / 180 * (angle + 2)), height / 2 + (radius * 0.93 - radius * 0.14) * sin(PI / 180 * (angle + 2)), width / 2 + radius * 0.94 * cos(PI / 180 * (angle + 2)), height / 2 + radius * 0.94 * sin(PI / 180 * (angle + 2)));
}

//按键旋转

void buttonhi(double width, double height, double radius)
{

    for (double i = 0; i < radius*0.05128205; i += 0.5) {
        setfillcolor(EGERGB(30, 30, 30));
        ege_fillcircle(width / 2 - 0.91 * radius + i, height / 2 + 1 * radius - i, radius * 0.1743589);//174.72     34
    }

    setcolor(EGERGB(50, 50, 50));
    double x1 = width / 2 - 0.91 * radius;
    double y1 = height / 2 + 1 * radius - 0.9 * radius * 0.10416 - 0.1875 * radius;
    double x2 = width / 2 - 0.91 * radius;
    double y2 = height / 2 + 1 * radius - 0.1875 * radius;
    //ege_line(x1, y1, x2, y2);

    x1 = width / 2 - 0.91 * radius + 0.1875 * radius * sin(PI / 180 * 43);
    y1 = height / 2 + 1 * radius - 0.9 * radius * 0.10416 - 0.1875 * radius * cos(PI / 180 * 43);
    x2 = width / 2 - 0.91 * radius + 0.1875 * radius * sin(PI / 180 * 43);
    y2 = height / 2 + 1 * radius - 0.1875 * radius * cos(PI / 180 * 43);
    //ege_line(x1, y1, x2, y2);

    x1 = width / 2 - 0.91 * radius - 0.1875 * radius * sin(PI / 180 * 43);
    y1 = height / 2 + 1 * radius - 0.9 * radius * 0.10416 - 0.1875 * radius * cos(PI / 180 * 43);
    x2 = width / 2 - 0.91 * radius - 0.1875 * radius * sin(PI / 180 * 43);
    y2 = height / 2 + 1 * radius - 0.1875 * radius * cos(PI / 180 * 43);
    //ege_line(x1, y1, x2, y2);
    setfillcolor(EGERGB(40, 40, 40));
    ege_fillcircle(width / 2 - 0.91 * radius, height / 2 + 1 * radius, radius * 0.1641025);//32
    settextjustify(CENTER_TEXT, CENTER_TEXT);
    setcolor(EGERGB(180, 180, 180));
    setfont(radius*0.087179487, radius*0.035897435, "Arial", 0, 0, 600, 0, 0, 0, 0);//17 7 600
    setbkmode(TRANSPARENT);
    outtextxy(width / 2 - 0.91 * radius, height / 2 + 1 * radius + radius * 0.0358974, "PUSH");

    //箭头
    setfillcolor(EGERGB(180, 180, 180));
    ege_point point1[] = { width / 2 - 0.91 * radius - radius * 0.102564,height / 2 + 1 * radius - radius * 0.06667,//20  13
                          width / 2 - 0.91 * radius - radius * 0.05641,height / 2 + 1 * radius - radius * 0.051282,//11 10
                          width / 2 - 0.91 * radius - radius * 0.092307,height / 2 + 1 * radius - radius * 0.0205128 };//18  4

    ege_point point2[] = { width / 2 - 0.91 * radius + radius * 0.102564,height / 2 + 1 * radius - radius * 0.06667,//20  13
                          width / 2 - 0.91 * radius + radius * 0.05641,height / 2 + 1 * radius - radius * 0.051282,//11  10
                          width / 2 - 0.91 * radius + radius * 0.092307,height / 2 + 1 * radius - radius * 0.0205128 };//18  4


    ege_fillpoly(3, point1);
    ege_fillpoly(3, point2);
    setlinewidth(radius * 0.0205128);//4
    ege_arc(width / 2 - 0.91 * radius - radius * 0.097435, height / 2 + 1 * radius - radius * 0.097435, radius * 0.097435 * 2, radius * 0.097435 * 2, 210, 120);//19  19




    for (double i = 0; i < radius * 0.05128205; i += 0.5) {
        setfillcolor(EGERGB(30, 30, 30));
        ege_fillcircle(width / 2 + 0.91 * radius - i, height / 2 + 1 * radius - i, radius * 0.1743589);//34
    }

    setcolor(EGERGB(50, 50, 50));
    x1 = width / 2 + 0.91 * radius;
    y1 = height / 2 + 1 * radius - 0.9 * radius * 0.10416 - 0.1875 * radius;
    x2 = width / 2 + 0.91 * radius;
    y2 = height / 2 + 1 * radius - 0.1875 * radius;
    //ege_line(x1, y1, x2, y2);

    x1 = width / 2 + 0.91 * radius + 0.1875 * radius * sin(PI / 180 * 43);
    y1 = height / 2 + 1 * radius - 0.9 * radius * 0.10416 - 0.1875 * radius * cos(PI / 180 * 43);
    x2 = width / 2 + 0.91 * radius + 0.1875 * radius * sin(PI / 180 * 43);
    y2 = height / 2 + 1 * radius - 0.1875 * radius * cos(PI / 180 * 43);
    //ege_line(x1, y1, x2, y2);

    x1 = width / 2 + 0.91 * radius - 0.1875 * radius * sin(PI / 180 * 43);
    y1 = height / 2 + 1 * radius - 0.9 * radius * 0.10416 - 0.1875 * radius * cos(PI / 180 * 43);
    x2 = width / 2 + 0.91 * radius - 0.1875 * radius * sin(PI / 180 * 43);
    y2 = height / 2 + 1 * radius - 0.1875 * radius * cos(PI / 180 * 43);
    //ege_line(x1, y1, x2, y2);

    setfillcolor(EGERGB(40, 40, 40));
    ege_fillcircle(width / 2 + 0.91 * radius, height / 2 + 1 * radius, radius * 0.1641025);
    settextjustify(CENTER_TEXT, CENTER_TEXT);
    setcolor(EGERGB(200, 75, 42));
    setfont(radius*0.0923076923, radius * 0.035897435, "Arial", 0, 0, 600, 0, 0, 0, 0);// 18 7 600
    setbkmode(TRANSPARENT);
    outtextxy(width / 2 + 0.91 * radius + radius * 0.0102564, height / 2 + 1 * radius + radius * 0.035897, "HDG");//2  7


    setfillcolor(EGERGB(200, 75, 42));
    ege_point point3[] = { width / 2 + 0.91 * radius - radius * 0.05128, height / 2 + 1 * radius - radius * 0.102564,//10   20
                            width / 2 + 0.91 * radius - radius * 0.010256, height / 2 + 1 * radius - radius * 0.102564,//2  20 
                            width / 2 + 0.91 * radius - radius * 0.010256, height / 2 + 1 * radius - radius * 0.0410256 ,//2  8
                            width / 2 + 0.91 * radius - radius * 0.005128, height / 2 + 1 * radius - radius * 0.020512,//1  4
                            width / 2 + 0.91 * radius - radius * 0.025641, height / 2 + 1 * radius - radius * 0.020512,//5  4
                            width / 2 + 0.91 * radius - radius * 0.05128, height / 2 + 1 * radius - radius * 0.05128 };//10  10

    ege_point point4[] = { width / 2 + 0.91 * radius + radius * 0.010256, height / 2 + 1 * radius - radius * 0.102564 ,//2 20
                            width / 2 + 0.91 * radius + radius * 0.05128, height / 2 + 1 * radius - radius * 0.102564,//10  20
                            width / 2 + 0.91 * radius + radius * 0.05128, height / 2 + 1 * radius - radius * 0.05128,//10  10
                            width / 2 + 0.91 * radius + radius * 0.025641, height / 2 + 1 * radius - radius * 0.020512,//5  4
                            width / 2 + 0.91 * radius + radius * 0.010256, height / 2 + 1 * radius - radius * 0.020512 };//2  4

    ege_fillpoly(6, point3);

    ege_fillpoly(5, point4);
}
//void specialPointOrange(double width, double height, double radius)
//{
//
//    for (double i = 1; i < radius * 0.10416; i += 0.5) {
//        setfillcolor(EGERGB(30, 30, 30));
//        ege_fillcircle(width / 2 - 0.91 * radius, height / 2 + 1 * radius - 0.9 * i, 0.1875 * radius);
//    }
//
//    setcolor(EGERGB(50, 50, 50));
//    double x1 = width / 2 - 0.91 * radius;
//    double y1 = height / 2 + 1 * radius - 0.9 * radius * 0.10416 - 0.1875 * radius;
//    double x2 = width / 2 - 0.91 * radius;
//    double y2 = height / 2 + 1 * radius - 0.1875 * radius;
//    ege_line(x1, y1, x2, y2);
//
//    x1 = width / 2 - 0.91 * radius + 0.1875 * radius * sin(PI / 180 * 43);
//    y1 = height / 2 + 1 * radius - 0.9 * radius * 0.10416 - 0.1875 * radius * cos(PI / 180 * 43);
//    x2 = width / 2 - 0.91 * radius + 0.1875 * radius * sin(PI / 180 * 43);
//    y2 = height / 2 + 1 * radius - 0.1875 * radius * cos(PI / 180 * 43);
//    ege_line(x1, y1, x2, y2);
//
//    x1 = width / 2 - 0.91 * radius - 0.1875 * radius * sin(PI / 180 * 43);
//    y1 = height / 2 + 1 * radius - 0.9 * radius * 0.10416 - 0.1875 * radius * cos(PI / 180 * 43);
//    x2 = width / 2 - 0.91 * radius - 0.1875 * radius * sin(PI / 180 * 43);
//    y2 = height / 2 + 1 * radius - 0.1875 * radius * cos(PI / 180 * 43);
//    ege_line(x1, y1, x2, y2);
//    setfillcolor(EGERGB(40, 40, 40));
//    ege_fillcircle(width / 2 - 0.91 * radius, height / 2 + 1 * radius, 0.1875 * radius);
//    settextjustify(CENTER_TEXT, CENTER_TEXT);
//    setcolor(WHITE);
//    setfont(21, 0, "Arial", 0, 0, 600, 0, 0, 0, 0);
//    setbkmode(TRANSPARENT);
//    outtextxy(width / 2 - 0.91 * radius, height / 2 + 1 * radius, "PUSH");
//
//    for (double i = 1; i < radius * 0.10416; i += 0.5) {
//        setfillcolor(EGERGB(30, 30, 30));
//        ege_fillcircle(width / 2 + 0.91 * radius, height / 2 + 1 * radius - 0.9 * i, 0.1875 * radius);
//    }
//
//    setcolor(EGERGB(50, 50, 50));
//    x1 = width / 2 + 0.91 * radius;
//    y1 = height / 2 + 1 * radius - 0.9 * radius * 0.10416 - 0.1875 * radius;
//    x2 = width / 2 + 0.91 * radius;
//    y2 = height / 2 + 1 * radius - 0.1875 * radius;
//    ege_line(x1, y1, x2, y2);
//
//    x1 = width / 2 + 0.91 * radius + 0.1875 * radius * sin(PI / 180 * 43);
//    y1 = height / 2 + 1 * radius - 0.9 * radius * 0.10416 - 0.1875 * radius * cos(PI / 180 * 43);
//    x2 = width / 2 + 0.91 * radius + 0.1875 * radius * sin(PI / 180 * 43);
//    y2 = height / 2 + 1 * radius - 0.1875 * radius * cos(PI / 180 * 43);
//    ege_line(x1, y1, x2, y2);
//
//    x1 = width / 2 + 0.91 * radius - 0.1875 * radius * sin(PI / 180 * 43);
//    y1 = height / 2 + 1 * radius - 0.9 * radius * 0.10416 - 0.1875 * radius * cos(PI / 180 * 43);
//    x2 = width / 2 + 0.91 * radius - 0.1875 * radius * sin(PI / 180 * 43);
//    y2 = height / 2 + 1 * radius - 0.1875 * radius * cos(PI / 180 * 43);
//    ege_line(x1, y1, x2, y2);
//
//    setfillcolor(EGERGB(40, 40, 40));
//    ege_fillcircle(width / 2 + 0.91 * radius, height / 2 + 1 * radius, 0.1875 * radius);
//    settextjustify(CENTER_TEXT, CENTER_TEXT);
//    setcolor(EGERGB(200, 75, 42));
//    setfont(21, 0, "Arial", 0, 0, 600, 0, 0, 0, 0);
//    setbkmode(TRANSPARENT);
//    outtextxy(width / 2 + 0.91 * radius, height / 2 + 1 * radius, "HDG");
//}

// 表盘刻度函数
void scale(double width, double height, double radius, double current_heading)
{
    int i = 0;

    for (i = 0; i <= 360; i += 5)
    {
        double x1;
        double y1;
        double x2;
        double y2;
        double actual_angle = i - current_heading;
        if (actual_angle < 0) actual_angle += 360;
        setcolor(EGERGB(190, 190, 190));
        LOGFONT f;

        getfont(&f);
        setfont(radius*0.102564102, 0, "Arial");//20
        f.lfHeight = radius * 0.188; // 设置字体高度//45
        f.lfWeight = radius*41.0256;// 设置字体粗细//8000
        f.lfQuality = PROOF_QUALITY;
        f.lfPitchAndFamily = FF_DECORATIVE;
        f.lfEscapement = -(actual_angle + 90.0) * 10;
        settextjustify(CENTER_TEXT, CENTER_TEXT);
        setbkmode(TRANSPARENT);

        //f.lfOrientation = (actual_angle ) * 10;

        if (i % 30 == 0)
        {
            x1 = width / 2 + radius * 0.92916 * cos(PI / 180 * actual_angle);//223
            y1 = height / 2 + radius * 0.92916 * sin(PI / 180 * actual_angle);
            x2 = width / 2 + (radius * 0.92916 - radius * 0.14583) * cos(PI / 180 * actual_angle);//（223-35）
            y2 = height / 2 + (radius * 0.92916 - radius * 0.14583) * sin(PI / 180 * actual_angle);
            setlinewidth(radius*0.0205128);//4
            ege_line(x1, y1, x2, y2);

            setfont(&f);


            if (i == 0)
            {
                //ege_outtextxy(width / 2 + (radius   - radius *0.2) * cos(PI / 180 * actual_angle),
                //height / 2 + (radius   - radius * 0.2) * sin(PI / 180 * actual_angle),"E");
            }
            if (i == 30)
            {
                ege_outtextxy(width / 2 + (radius - radius * 0.2) * cos(PI / 180 * actual_angle),
                    height / 2 + (radius - radius * 0.2) * sin(PI / 180 * actual_angle), "12");
            }
            if (i == 60)
            {
                ege_outtextxy(width / 2 + (radius - radius * 0.2) * cos(PI / 180 * actual_angle),
                    height / 2 + (radius - radius * 0.2) * sin(PI / 180 * actual_angle), "15");
            }
            if (i == 90)
            {
                ege_outtextxy(width / 2 + (radius - radius * 0.2) * cos(PI / 180 * actual_angle),
                    height / 2 + (radius - radius * 0.2) * sin(PI / 180 * actual_angle), "S");
            }
            if (i == 120)
            {
                ege_outtextxy(width / 2 + (radius - radius * 0.2) * cos(PI / 180 * actual_angle),
                    height / 2 + (radius - radius * 0.2) * sin(PI / 180 * actual_angle), "21");
            }
            if (i == 150)
            {
                ege_outtextxy(width / 2 + (radius - radius * 0.2) * cos(PI / 180 * actual_angle),
                    height / 2 + (radius - radius * 0.2) * sin(PI / 180 * actual_angle), "24");
            }if (i == 180)
            {
                ege_outtextxy(width / 2 + (radius - radius * 0.2) * cos(PI / 180 * actual_angle),
                    height / 2 + (radius - radius * 0.2) * sin(PI / 180 * actual_angle), "W");
            }
            if (i == 210)
            {
                ege_outtextxy(width / 2 + (radius - radius * 0.2) * cos(PI / 180 * actual_angle),
                    height / 2 + (radius - radius * 0.2) * sin(PI / 180 * actual_angle), "30");
            }
            if (i == 240)
            {
                ege_outtextxy(width / 2 + (radius - radius * 0.2) * cos(PI / 180 * actual_angle),
                    height / 2 + (radius - radius * 0.2) * sin(PI / 180 * actual_angle), "33");
            }
            if (i == 270)
            {
                ege_outtextxy(width / 2 + (radius - radius * 0.2) * cos(PI / 180 * actual_angle),
                    height / 2 + (radius - radius * 0.2) * sin(PI / 180 * actual_angle), "N");
            }
            if (i == 300)
            {
                ege_outtextxy(width / 2 + (radius - radius * 0.2) * cos(PI / 180 * actual_angle),
                    height / 2 + (radius - radius * 0.2) * sin(PI / 180 * actual_angle), "3");
            }
            if (i == 330)
            {
                ege_outtextxy(width / 2 + (radius - radius * 0.2) * cos(PI / 180 * actual_angle),
                    height / 2 + (radius - radius * 0.2) * sin(PI / 180 * actual_angle), "6");
            }
            if (i == 360)
            {
                ege_outtextxy(width / 2 + (radius - radius * 0.2) * cos(PI / 180 * actual_angle),
                    height / 2 + (radius - radius * 0.2) * sin(PI / 180 * actual_angle), "E");
            }
            // 设置新的字体

        }
        else if (i % 10 == 0)
        {
            x1 = width / 2 + radius * 0.92916 * cos(PI / 180 * actual_angle);//223
            y1 = height / 2 + radius * 0.92916 * sin(PI / 180 * actual_angle);
            x2 = width / 2 + (radius * 0.92916 - radius * 0.10416) * cos(PI / 180 * actual_angle);//（223-25）
            y2 = height / 2 + (radius * 0.92916 - radius * 0.10416) * sin(PI / 180 * actual_angle);
            setlinewidth(4);
            setlinewidth(radius*0.01025641);//2
            ege_line(x1, y1, x2, y2);
        }
        else if (i % 5 == 0)
        {
            x1 = width / 2 + radius * 0.92916 * cos(PI / 180 * actual_angle);//223
            y1 = height / 2 + radius * 0.92916 * sin(PI / 180 * actual_angle);
            x2 = width / 2 + (radius * 0.92916 - radius * 0.0625) * cos(PI / 180 * actual_angle);//（223-15）
            y2 = height / 2 + (radius * 0.92916 - radius * 0.0625) * sin(PI / 180 * actual_angle);
            setlinewidth(4);
            setlinewidth(radius * 0.01025641);
            ege_line(x1, y1, x2, y2);
        }

    }
}
//模拟数据输入
//double  update_heading(double current_heading) {
//	static int direction = 1;
//	//current_heading += direction *1.5;
//	key_msg msg;
//	msg = getkey();
//
//	if (msg.key == key_left) {
//		current_heading -= direction * 1.5;
//	}
//	else if (msg.key == key_right) {
//		current_heading += direction * 1.5;
//	}
//
//
//
//	//小球
//		//if (current_heading >= 360) direction = -1;
//		//if (current_heading <= 0) direction = 1;
//	return current_heading;
//}

//double  update_heading(double angle) {

//}

void screw(double DIAL_RADIUS, double CENTER_X, double CENTER_Y)//螺丝
{
    // 使用float保证计算精度（根据需求可改为double）
    const float DIAL_RADIUS_F = (float)DIAL_RADIUS;
    const float CENTER_X_F = (float)CENTER_X;
    const float CENTER_Y_F = (float)CENTER_Y;

    // 参数化定义（精确到小数点后6位）
    const float SCREW_OFFSET = DIAL_RADIUS_F * 0.055555556f; // 1/18
    const float ELLIPSE_OFFSET_X = -DIAL_RADIUS_F * 0.111111111f; // -1/9
    const float ELLIPSE_OFFSET_Y = -DIAL_RADIUS_F * 0.166666667f; // -1/6
    const float ELLIPSE_SIZE = DIAL_RADIUS_F * 0.277777778f;  // 5/18
    const float RECT_OFFSET_X = DIAL_RADIUS_F * 0.222222222f;  // 2/9
    const float RECT_OFFSET_Y = DIAL_RADIUS_F * 0.333333333f;  // 1/3
    const float RECT_WIDTH = DIAL_RADIUS_F * 0.555555556f;   // 5/9
    const float RECT_HEIGHT = DIAL_RADIUS_F * 0.611111111f;  // 11/18
    const float CROSS_OFFSET1 = DIAL_RADIUS_F * 0.055555556f;  // 1/18
    const float CROSS_OFFSET2 = DIAL_RADIUS_F * 0.027777778f;  // 1/36
    const float LINE_WIDTH = DIAL_RADIUS_F * 0.016666667f;  // 1/60

    ege_enable_aa(TRUE);

    // 绘制函数（使用浮点坐标）
    auto draw_screw = [&](float cx, float cy) {
        // 精确到小数点后6位的渐变参数
        ege_setpattern_ellipsegradient(
            ege_point{ roundf(cx), roundf(cy) }, // 最终转整数
            EGEACOLOR(0xFF, BLACK),
            roundf(cx + ELLIPSE_OFFSET_X),
            roundf(cy + ELLIPSE_OFFSET_Y),
            roundf(ELLIPSE_SIZE),
            roundf(ELLIPSE_SIZE),
            EGEACOLOR(0, WHITE)
        );
        ege_fillrect(
            roundf(cx - RECT_OFFSET_X),
            roundf(cy - RECT_OFFSET_Y),
            roundf(RECT_WIDTH),
            roundf(RECT_HEIGHT)
        );

        // 灰色渐变层（精确尺寸调整）
        const float adjusted_size = ELLIPSE_SIZE - DIAL_RADIUS_F * 0.055555556f; // 替代原-10
        ege_setpattern_ellipsegradient(
            ege_point{
                    roundf(cx - SCREW_OFFSET * 0.5f),
                    roundf(cy - SCREW_OFFSET * 0.5f)
            },
            EGEACOLOR(0xFF, EGERGB(170, 170, 170)),
            roundf(cx + ELLIPSE_OFFSET_X),
            roundf(cy + ELLIPSE_OFFSET_Y),
            roundf(adjusted_size),
            roundf(adjusted_size),
            EGEACOLOR(0xFF, EGERGB(20, 20, 20))
        );
        ege_fillrect(
            roundf(cx - RECT_OFFSET_X),
            roundf(cy - RECT_OFFSET_Y),
            roundf(RECT_WIDTH),
            roundf(RECT_HEIGHT)
        );

        // 十字线（亚像素级精度）
        setcolor(EGERGB(30, 30, 30));
        setlinewidth(roundf(LINE_WIDTH));

        const float cross_y = cy - CROSS_OFFSET2 * 2.0f;
        ege_line(
            roundf(cx - CROSS_OFFSET1), roundf(cross_y),
            roundf(cx + CROSS_OFFSET1), roundf(cross_y)
        );

        const float cross_x = cx;
        const float cross_y_start = cy + CROSS_OFFSET2 / 4;
        const float cross_y_end = cy - CROSS_OFFSET2 * 4.0f;
        ege_line(
            roundf(cross_x), roundf(cross_y_start),
            roundf(cross_x), roundf(cross_y_end)
        );
        };

    // 计算四个螺丝中心坐标（浮点运算）
    const float screw_radius = DIAL_RADIUS_F - SCREW_OFFSET;

    // 左上
    draw_screw(
        CENTER_X_F - screw_radius,
        CENTER_Y_F - DIAL_RADIUS_F
    );

    // 右上
    draw_screw(
        CENTER_X_F + screw_radius,
        CENTER_Y_F - DIAL_RADIUS_F
    );

    // 右下
    //draw_screw(
    //        CENTER_X_F + screw_radius,
    //        CENTER_Y_F + DIAL_RADIUS_F
    //);

    //// 左下（新增）
    //draw_screw(
    //        CENTER_X_F - screw_radius,
    //        CENTER_Y_F + DIAL_RADIUS_F
    //);
}
